<?php

namespace App\Http\Controllers\Admin;

use App\Home\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Admin\Send;
class MemberController extends Controller
{
//    会员列表显示
    public function member()
    {
        $result=DB::table('sends')->paginate(1);
        return view('admin.member.member')->with('result',$result);

    }
    //    多条件查询
    public function memberdata(Request $request)
    {
//        获取查询条件
        $username = isset($request->username) ? $request->username : '';
        $where=1;
//        拼接查询条件
        if ($username != '') {
            $where .="   and username like '%{$username}%'";
        }
//查询并且分页
        $result=DB::table('sends')->whereRaw($where)->paginate(1);
        return view('admin.member.member')->with('result',$result);
    }

//    会员信息发送状态修改
    public function changestatus(Request $request)
    {
        $input = $request->all();
        $member = Send::find($input['id']);
        $member->status=($member->status==1?2:1);
        $res = $member->save();
        if($res){
            $data = [
                'status'=>0,
                'msg'=>'亲，消息发送成功喽^*^',
            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'消息发送失败，请重试',
            ];
        }
        return $data;
    }
//    消息保存
    public function news(Request $request)
    {
        $news=$request->news;
        $user_id=$request->user_id;
        $data=Users::find($user_id);
        $username=$data->username;
        $phone=$data->phone;
        if($news!=''){
            $result=DB::table('sends')->insert([
                'user_id'=>$user_id,
                'username'=>$username,
                'phone'=>$phone,
                'news'=>$news,
            ]);
        }
  return redirect('admin/Member');
    }

//    删除要发送的消息
    public function newsdel($id)
    {
        $res = Send::where('id',$id)->delete();
        return redirect('admin/Member');
    }
}
